// pages/VIPmanage/VIPmanage.js
const db = wx.cloud.database()
const app = getApp()
let sName = ''
var util= require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['会员名单', '会员审核'],
    currentTab: 0,
    userRole: 1,
    addPhone: "",
    addID: "",
    delPhone: "",
    delID: "",
    VIP: [],
    user: [],
    addModalHidden: true,
    delModalHidden: true,
    listData: [],

    waitAuditList:[],
  },

  //联表查询
  get_db() {
    var that = this
    wx.cloud.callFunction({
      name: "lookupVIP",
      data: {},
      success(res) {
        var a = []
        for (var i = 0; i < res.result.list.length; i++) {
          a.push(res.result.list[i])
        }
        that.setData({
          listData: a
        })
      }
    })
  },
  //模糊查询
  getSearchName: function (e) {
    sName = e.detail.value
  },
  // searchName:function(){
  //   var that = this
  //   db.collection('User').where({
  //     //使用正则查询，实现对搜索的模糊查询
  //     name: db.RegExp({
  //       regexp: sName,
  //       //从搜索栏中获取的value作为规则进行匹配。
  //       options: 'i',
  //       //大小写不区分
  //     })
  //   }).get({
  //     success: res => {
  //       console.log(res)
  //       that.setData({
  //         listData: res.data
  //       })
  //     }
  //   })
  // },
  searchName(listData, sName) {
    var that = this
    var arr = [];
    var listData1 = that.data.listData
    for (var i = 0; i < listData1.length; i++) {
      if (listData1[i].indexOf(sName) >= 0) {
        arr.push(listData1[i]);
      }
    }

    // return arr;
    that.setData({
      listData: arr
    })

  },



  //切换bar
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
    //全局变量
    app.globalData.currentTab = e.currentTarget.dataset.idx;
  },

  //超管添加管理员
  showAdd() {
    this.setData({
      addModalHidden: false
    })
  },

  addTag(e) {
    this.setData({
      addPhone: e.detail.value
    })
  },

  addManager() {
    var that = this
    db.collection("User").where({
      phoneNumber: that.data.addPhone
    }).get({
      success(res) {
        that.setData({
          addID: res.data[0].openid
        })
        db.collection("Role").where({
          userID: that.data.addID
        }).get().then(res => {
          if (res.data.length != 0) {
            wx.showToast({
              title: '该用户已经是管理员了哦',
              icon: 'none',
              duration: 1000
            });
          } else {
            db.collection("Role")
              .add({
                data: {
                  type: "1",
                  userID: that.data.addID
                },
                success(res) {
                  console.log("添加管理员成功", res)
                  wx.showToast({
                    title: '添加管理员成功',
                    icon: 'success',
                    duration: 1000
                  });
                },
                fail: function (res) {
                  console.log("添加管理员失败", res)
                }
              })
          }
        })
      },
      fail: function (res) {
        console.log(res)
        wx.showToast({
          title: '查无此手机号',
          duration: 1000
        });
      }
    })
    this.setData({
      addModalHidden: true
    })
  },

  addModalCandel() {
    this.setData({
      addModalHidden: true
    })
  },

  //超管删除管理员
  showDel() {
    this.setData({
      delModalHidden: false
    })
  },
  delTag(e) {
    this.setData({
      delPhone: e.detail.value
    })
  },

  delManager() {
    var that = this
    db.collection("User").where({
      phoneNumber: that.data.delPhone
    }).get({
      success(res) {
        that.setData({
          delID: res.data[0].openid
        })
        db.collection("Role").where({
          userID: that.data.delID
        }).remove({
          success: function (res) {
            console.log("删除成功", res)
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 1000
            });
          },
          fail: function (res) {
            console.log("删除失败", res)
            wx.showToast({
              title: '删除失败，请检查输入',
              icon: 'none',
              duration: 1000
            });
          }
        })
      }
    })
    this.setData({
      delModalHidden: true
    })
  },

  delModalCandel() {
    this.setData({
      delModalHidden: true
    })
  },


  getwaitAuditList(){
    db.collection("Identifies").where({
      status:"0"
    }).get().then(res =>{
      console.log(9999)
      let time = 0
      for(var i = 0;i<res.data.length;i++){
        time= util.formatTime(new Date(res.data[i].date))
        res.data[i].date = time
      }    
      this.setData({
        waitAuditList:res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.get_db();
    const ui = wx.getStorageSync("userInfo")
    db.collection('Role').where({
      userID:ui.openid
    }).get().then(res =>{
      this.setData({
        userRole: parseInt(res.data[0].type)
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      currentTab: app.globalData.currentTab
    })
    this.getwaitAuditList()


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.get_db();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})